#include "main.h"

struct Ball balls;
struct Bar bars;
struct Block blocks[50];

SDL_Surface* background;
SDL_Surface* ballI;
SDL_Surface* barI;
SDL_Surface* shortbarI;
SDL_Surface* largebarI;
SDL_Surface* blockT1I; //Normal, blanco
SDL_Surface* blockT2I; //Agranda, azul
SDL_Surface* blockT3I; //Encoge, rojo
//SDL_Surface* blockT4I; //Pistola, verde
SDL_Surface* blockT5I; //2 golpes, amarillo
SDL_Surface* blockT6I; //irrompible, marron
SDL_Surface* giftT1I; //Agranda, azul
SDL_Surface* giftT2I; //Encoge, rojo
//SDL_Surface* giftT3I; //Pistola, verde

void createGift(struct Gift gift) {
	if (gift.giftView == 1) {
		if (gift.giftType == 1) {
			apply_s(gift.giftX, gift.giftY, giftT1I, screens, NULL);
		}
		if (gift.giftType == 2) {
			apply_s(gift.giftX, gift.giftY, giftT2I, screens, NULL);
		}
		/**if (gift.giftType == 3) {
			apply_s(gift.giftX, gift.giftY, giftT3I, screens, NULL);
        }*/
		if (gift.giftY > 460 && (gift.giftX > bars.barX && gift.giftX < bars.barX + bars.barWidth)) {
			if (gift.giftType == 1) {
				bars.barWidth = 70;
			}
			if (gift.giftType == 2) {
				bars.barWidth = 30;
			}
			/**if (gift.giftType == 3) {
				bars.barWidth = 50;
				bars.barGun++;
				if (bars.barGun >= 3) {
					bars.barGun = 1;
				}
			}*/
			gift.giftView = 0;
		}
		if (gift.giftY > 475) {
			gift.giftView = 0;
		}
		gift.giftY =+ 5;
	}
}

void createBlock(struct Block block) {
	struct Gift gift;
	if (block.blockView == 1) {
		if (block.blockType == 1) {
			apply_s(block.blockX, block.blockY, blockT1I, screens, NULL);
		}
		if (block.blockType == 2) {
			apply_s(block.blockX, block.blockY, blockT2I, screens, NULL);
		}
		if (block.blockType == 3) {
			apply_s(block.blockX, block.blockY, blockT3I, screens, NULL);
		}
		/**if (block.blockType == 4) {
			apply_s(block.blockX, block.blockY, blockT4I, screens, NULL);
		}*/
		if (block.blockType == 5) {
			apply_s(block.blockX, block.blockY, blockT5I, screens, NULL);
		}
		if (block.blockType == 6) {
			apply_s(block.blockX, block.blockY, blockT6I, screens, NULL);
		}
		int collision = 0;
		if ((balls.ballX > block.blockX && balls.ballX < block.blockX+60) && (balls.ballY <= block.blockY+20 && balls.ballY >= block.blockY+10)) {
			collision = 1;
			balls.ballVelY = 2;
		}
		if ((balls.ballX > block.blockX && balls.ballX < block.blockX+60) && (balls.ballY >= block.blockY && balls.ballY <= block.blockY+10 )) {
			collision = 1;
			balls.ballVelY = -2;
		}
		/**if ((balax[pistola] > xinit && balax[pistola] < xinit+30) && (balay[pistola] < yinit+6 && balay[pistola] > yinit)) {
			collision = 1;
			mostrarbala = 0;
		}*/
		if (collision == 1) {
			if(block.blockType != 6) {
				block.blockCollisions = block.blockCollisions-1;
			}
			if (block.blockCollisions == 0) {
				block.blockView = 0;
			}
			if (block.blockType == 2 || block.blockType == 3 || block.blockType == 4) {
				gift.giftView = 1;
				gift.giftX = block.blockX+25;
				gift.giftY = block.blockY;
				gift.giftType = block.blockType-1;
				createGift(gift);
			}
		}
	}
}

void drawBlock() {
	int i;
	for (i=1; i<=50; i++) {
		createBlock(blocks[i]);
	}
}

void setBlock(int numberblock, int type, int xinit, int yinit) {
	blocks[numberblock].blockX = xinit;
	blocks[numberblock].blockY = yinit;
	blocks[numberblock].blockType = type;
	if (type == 5) {
		blocks[numberblock].blockCollisions = 2;
	} else {
		blocks[numberblock].blockCollisions = 1;
	}
	blocks[numberblock].blockView = 1;
}

void generarBlocks() {
	int numerofilas = GenAlt(3, 5);
	int i, y = 10;
	for( i=0; i<= numerofilas; i++) {
		int tipo = GenAlt(1, 5);
		int numblock = 0;
		if (tipo == 1) { //12 bloques
			setBlock(numblock+1, 1, 20, y);
			setBlock(numblock+2, 1, 80, y);
			setBlock(numblock+3, 1, 140, y);
			setBlock(numblock+4, 1, 200, y);
			setBlock(numblock+5, 1, 260, y);
			setBlock(numblock+6, 1, 320, y);
			setBlock(numblock+7, 1, 380, y);
			setBlock(numblock+8, 1, 440, y);
			setBlock(numblock+9, 1, 500, y);
			setBlock(numblock+10, 1, 560, y);
			numblock = numblock + 10;
		} else if (tipo == 2) { // 5 bloques par
			setBlock(numblock+1, 1, 80, y);
			setBlock(numblock+2, 1, 200, y);
			setBlock(numblock+3, 1, 320, y);
			setBlock(numblock+4, 1, 440, y);
			setBlock(numblock+5, 1, 560, y);
			numblock = numblock + 5;
		} else if (tipo == 3) { //5 bloques impar
			setBlock(numblock+1, 1, 20, y);
			setBlock(numblock+2, 1, 140, y);
			setBlock(numblock+3, 1, 260, y);
			setBlock(numblock+4, 1, 380, y);
			setBlock(numblock+5, 1, 500, y);
			numblock = numblock + 5;
		} else if (tipo == 4) { //6 bloques centro y orilla
			setBlock(numblock+1, 1, 20, y);
			setBlock(numblock+2, 1, 80, y);
			setBlock(numblock+3, 1, 260, y);
			setBlock(numblock+4, 1, 320, y);
			setBlock(numblock+5, 1, 500, y);
			setBlock(numblock+6, 1, 560, y);
			numblock = numblock + 6;
		} else if (tipo == 5) { //4 bloques centro
			setBlock(numblock+1, 1, 200, y);
			setBlock(numblock+2, 1, 260, y);
			setBlock(numblock+3, 1, 320, y);
			setBlock(numblock+4, 1, 380, y);
			numblock = numblock + 4;
		}
		y = y + 30;
	}
}

void iniciarSettings() {
	balls.ballX = 310;
	balls.ballY = 230;
	balls.ballVelX = 0;
	balls.ballVelY = 2;
	bars.barX = 270;
	bars.barWidth = 100;
	GenSeed();
	generarBlocks();
}

void ball() {
	if (balls.ballX < 20) {
		balls.ballVelX = 2;
	}
	if (balls.ballY < 10) {
		balls.ballVelY = 2;
	}
	if (balls.ballX > 610) {
		balls.ballVelX = -2;
	}
	if (balls.ballY > 475) {
		iniciarSettings();
	}
	balls.ballX = balls.ballX+balls.ballVelX;
	balls.ballY = balls.ballY+balls.ballVelY;
	apply_s(balls.ballX-10, balls.ballY, ballI, screens, NULL);
}

void bar() {
	if(bars.barWidth == 100) {
		if (bars.barX < 20) {
			bars.barX = 20;
		}
		if (bars.barX > 510) {
			bars.barX = 510;
		}
		if (balls.ballY > 462 && (balls.ballX >= bars.barX && balls.ballX < bars.barX+20)) {
			balls.ballVelY = -1;
			balls.ballVelX = -4;
		}
		if (balls.ballY > 461 && (balls.ballX >= bars.barX+20 && balls.ballX < bars.barX+40)) {
			balls.ballVelY = -2;
			balls.ballVelX = -2;
		}
		if (balls.ballY > 460 && (balls.ballX >= bars.barX+40 && balls.ballX < bars.barX+60)) {
			balls.ballVelY = -2;
		}
		if (balls.ballY > 461 && (balls.ballX >= bars.barX+60 && balls.ballX < bars.barX+80)) {
			balls.ballVelY = -2;
			balls.ballVelX = 2;
		}
		if (balls.ballY > 462 && (balls.ballX >= bars.barX+80 && balls.ballX <= bars.barX+100)) {
			balls.ballVelY = -1;
			balls.ballVelX = 4;
		}
		apply_s(bars.barX, 460, barI, screens, NULL);
	}
	if(bars.barWidth == 60) {
		if (bars.barX < 20) {
			bars.barX = 20;
		}
		if (bars.barX > 550) {
			bars.barX = 550;
		}
		if (balls.ballY >= 462 && (balls.ballX >= bars.barX && balls.ballX < bars.barX+20)) {
			balls.ballVelY = -1;
			balls.ballVelX = -4;
		}
		if (balls.ballY >= 460 && (balls.ballX >= bars.barX+20 && balls.ballX < bars.barX+40)) {
			balls.ballVelY = -2;
		}
		if (balls.ballY >= 462 && (balls.ballX >= bars.barX+40 && balls.ballX <= bars.barX+60)) {
			balls.ballVelY = -2;
			balls.ballVelX = 2;
		}
		apply_s(bars.barX, 460, shortbarI, screens, NULL);
	}
	if(bars.barWidth == 140) {
		if (bars.barX < 20) {
			bars.barX = 20;
		}
		if (bars.barX > 470) {
			bars.barX = 470;
		}
		if (balls.ballY >= 463 && (balls.ballX >= bars.barX && balls.ballX < bars.barX+20)) {
			balls.ballVelY = -1;
			balls.ballVelX = -4;
		}
		if (balls.ballY >= 462 && (balls.ballX >= bars.barX+20 && balls.ballX < bars.barX+40)) {
			balls.ballVelY = -2;
			balls.ballVelX = -2;
		}
		if (balls.ballY >= 461 && (balls.ballX >= bars.barX+40 && balls.ballX < bars.barX+60)) {
			balls.ballVelY = -2;
			balls.ballVelX = -1;
		}
		if (balls.ballY >= 460 && (balls.ballX >= bars.barX+60 && balls.ballX < bars.barX+80)) {
			balls.ballVelY = -2;
		}
		if (balls.ballY >= 461 && (balls.ballX >= bars.barX+80 && balls.ballX < bars.barX+100)) {
			balls.ballVelY = -2;
			balls.ballVelX = 1;
		}
		if (balls.ballY >= 462 && (balls.ballX >= bars.barX+100 && balls.ballX < bars.barX+120)) {
			balls.ballVelY = -2;
			balls.ballVelX = 2;
		}
		if (balls.ballY >= 463 && (balls.ballX >= bars.barX+120 && balls.ballX < bars.barX+140)) {
			balls.ballVelY = -1;
			balls.ballVelX = 4;
		}
		apply_s(bars.barX, 460, largebarI, screens, NULL);
	}
	if (bars.barWidth != 100) {
		bars.barTimer = bars.barTimer-1;
		if (bars.barTimer == 1000) {
			bars.barTimer = 1000;
			bars.barWidth = 100;
		}
	}
	if (opt == 1) {
		if (pression & ButtonRight) {
			bars.barX = bars.barX + 3;
		}
		if (pression & ButtonLeft) {
			bars.barX = bars.barX - 3;
		}
		if (pressed & ButtonDown) {
			bars.barX = bars.barX + 3;
		}
		if (pressed & ButtonUp) {
			bars.barX = bars.barX - 3;
		}
	} else if (opt == 2) {
		bars.barX = ir.x;
	} else if (opt == 3) {
		wpadGetOrientation(Wpad1);
		if (orient.pitch < -10) {
			bars.barX = bars.barX + 3;
		}
		if (orient.pitch > 10) {
			bars.barX = bars.barX - 3;
		}
	}
}

int main(int argc, char *argv[]) {
	Init();
	screenFontLoad("error.ttf", 16);
	background = IMG_Load("fondo.png");
	ballI = IMG_Load("ball.png");
	barI = IMG_Load("bar.png");
	shortbarI = IMG_Load("shorbar.png");
	largebarI = IMG_Load("largebar.png");
	blockT1I = IMG_Load("blockt1.png");
    blockT2I = IMG_Load("blockt2.png");
    blockT3I = IMG_Load("blockt3.png");
    //blockT4I = IMG_Load("blockt4.png");
    blockT5I = IMG_Load("blockt5.png");
    blockT6I = IMG_Load("blockt6.png");
    giftT1I = IMG_Load("gift1.png");
    giftT2I = IMG_Load("gift2.png");
    //giftT3I = IMG_Load("blockt3.png");
	
    iniciarSettings();
	
	while (1) {
		wpadInitButtons(Wpad1);
		wpadInitIR(Wpad1);
		screenClear(COLORBLACK);
		apply_s(0, 0, background, screens, NULL);
		if (opt == 0) {
			//menu
			opt = 1;
		} else {
			bar();
			ball();
			drawBlock();
		}
		screenFlip();
	}
	return 0;
}


